<?php

namespace app\api\model;

use app\common\enums\StatusEnum;

class Question extends \app\common\model\Question
{
    /**
     * 常见问题列表
     * @param $page
     * @param $size
     * @return array
     * @throws \think\Exception\DbException
     */
    public static function getList($page, $size)
    {
        $offset = ($page - 1) * $size;
        $sql = static::where(['status' => StatusEnum::ENABLED]);

        $model = $sql->field('id, question')
            ->order(['sort_num' => 'desc', 'create_time' => 'desc'])
            ->limit($offset, $size)
            ->select()
            ->toArray();

        return [
            'list' => $model,
            'count' => $sql->count(),
        ];
    }

    /**
     * 常见问题列表详情
     * @param $id
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getOne($id)
    {
        $model = static::field('id, question, content,create_time')
            ->where(['id' => $id, 'status' => StatusEnum::ENABLED])
            ->find();
        return $model ? $model : [];
    }
}